package cn.tedu.lucky.draw.server.job.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;

public class ActivityDBToCsvJobListener implements JobExecutionListener {
    private static final Logger logger= LoggerFactory.getLogger(ActivityDBToCsvJobListener.class);

    /**
     * jobExecution 代表了当前正在执行的批处理作业恶的实例，它提供对作业执行过程中各种信息的状态和访问
     */
    @Override
    public void beforeJob(JobExecution jobExecution) {
        long begin = System.currentTimeMillis();
        /**
         * getExecutionContext() 方法返回一个 Map，用于保存作业执行过程中需要传递的数据，
         * 用于在作业执行或者步骤执行过程中保持和传递数据，允许开发者在作业不同的阶段之间共享数据
         */
        jobExecution.getExecutionContext().putLong("begin",begin);
        logger.info("开始执行,当前时间是:{}",begin);
    }

    @Override
    public void afterJob(JobExecution jobExecution) {
        long end = System.currentTimeMillis();
        logger.info("执行结束,当前时间是:{}",end);
        Long begin = jobExecution.getExecutionContext().getLong("begin");
        logger.info("本次执行耗时:{}毫秒",end-begin);
    }
}
